/*
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.iisageek.client;

import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.iisageek.client.widgets.sortabletable.OfficersTable;
import com.iisageek.client.widgets.xml.Node;
import com.iisageek.client.widgets.xml.Server;
import com.iisageek.client.widgets.xml.XMLHandler;

/**
 * MembersPanel
 * 
 * the dynamic panel that handles most functions under the "members" section
 * 
 * Singleton.
 * 
 * @author Bob Breznak
 */
public class MembersPanel extends FlowPanel{
	
	private static MembersPanel singleton;
	private static final NewMember nm = new NewMember();
	
	/**
	 * MembersPanel
	 * 
	 * sets the instance of MembersPanel to the singleton
	 */
	public MembersPanel() {
		singleton = this;
	}
	
	/**
	 * setMode
	 * 
	 * determines what functionality to display
	 * 
	 * @param mode (String) the function to display
	 */
	public void setMode(String mode) {
		clear();
		if(mode.equalsIgnoreCase(Shortcuts.MEMBERS_TAG + "newmember")) {
			nm.clear();
			add(nm);
		} else {
			lnl.get().showError();
		}
	}
	
	/**
	 * get
	 * 
	 * Returns the single instance of MembersPanel
	 * 
	 * @return MembersPanel
	 */
	public static MembersPanel get() {	
		return singleton;
	}

	/**
	 * ListOfficers
	 * 
	 * list all of the officers and the advisor
	 * 
	 * @author Bob Breznak
	 */	
	private static class ListOfficers implements XMLHandler {
		private String heading;
		private OfficersTable officers = new OfficersTable();
		
		/**
		 * ListOfficers
		 * 
		 * let the instance refresh it's data
		 */
		public ListOfficers() {
			refresh();
		}
		

		public Widget generateWidget() {
			Label tableHeading = new Label("Officers");
			tableHeading.setStyleName(".tableHeader");
			VerticalPanel panel = new VerticalPanel();
			panel.add(tableHeading);
			panel.add(officers.generateTable());
			return panel;
		}
		
		public void refresh() {
			lnl.get().showLoading();
			Server.requestData("http://lnl.wpi.edu/database_test/get.pl?filter=officers", this);
		}

		public void receivedData(Node root) {
			if(root == null) { return; }	
			Node tmp;
			if(root.getAttribute("result").equalsIgnoreCase("success")) {
				for (int i = 0; i < root.getChildren().length; i++) {
					tmp = root.getChild(i);		
					
					officers.add(new Member(root));
				}
				
				officers.formatTable();
				officers.hideLoading();
			}
		}
		
	}
	
	/**
	 * NewMember
	 * 
	 * allows the user to add a new member to the database.
	 * 
	 * @author Bob Breznak
	 *
	 */ 
	private static class NewMember extends Composite implements ClickListener, XMLHandler {
		//TODO make it work!
		
		private final VerticalPanel form = new VerticalPanel();
		
		//private final VNameTextBox first = new VNameTextBox("First", true,"80px");
		//private final VNameTextBox last = new VNameTextBox("Last", true,"80px");
		//private final VEmailTextBox email = new VEmailTextBox("Email",true,"80px");
		//private final VWPIIDBox wpiid = new VWPIIDBox("WPI ID",true,"80px");
		//private final VUploadBox photo = new VUploadBox("Photo", true,"80px");
		//private final VYearDropDown year = new VYearDropDown("Year",true,"80px");
		private final Button submit = new Button("Add User");
		//private final VDateTextBox cal = new VDateTextBox("Joined",true,"80px");
		
		public void clear() {
			
		}
		
		public NewMember() {
			submit.setEnabled(true);
			
			//form.add(first);
			//form.add(last);
			//form.add(email);
			//form.add(wpiid);
			//form.add(photo);
			//form.add(year);
			//form.add(cal);
			form.add(submit);
			
			submit.addClickListener(this);
			
			initWidget(form);
		}
		
		public void onClick(Widget sender) {
			
			String buffer="filter=addmember&";
			
			//first.validate();
			//last.validate();
			//email.validate();
			//wpiid.validate();
			//photo.validate();
			//year.validate();
			//cal.validate();
			
			//buffer+= "first="+first.toString()+"&";
			//buffer+= "last="+last.toString()+"&";
			//buffer+= "email="+email.toString()+"&";
			//buffer+= "wpiid="+wpiid.toString()+"&";
			//buffer+= "photo="+photo.toString()+"&";
			//buffer+= "year="+year.toString()+"&";
			//buffer+= "join="+cal.toURL();
			

			buffer+= "first=test&";
			buffer+= "last=test&";
			buffer+= "email=test@wpi.edu&";
			buffer+= "wpiid=test&";
			buffer+= "photo=test&";
			buffer+= "year=1234&";
			buffer+= "join=19870503";
			
			
			//if(first.isValid() && last.isValid() && email.isValid() && year.isValid() && cal.isValid()) {
			Server.sendData(lnl.get().config.getServer() + "send.pl", buffer, this);
			submit.setEnabled(false);
			//}
		}

		public void receivedData(Node root) {
			if(root.attributeExists("result")) {
				if(root.getAttribute("result").equals("added")){
					//AboutDialog dlg = new AboutDialog("New Member", first.toString() + " " + last.toString() + " has been added");
					//int left = (Window.getClientWidth() - 512) / 2;
					//int top = (Window.getClientHeight() - 256) / 2;
					//dlg.setPopupPosition(left, top);
					//dlg.show();
				} else {
					lnl.get().showError();
				}
			} else {
				lnl.get().showError();
			}
			submit.setEnabled(true);
		}

	}
}
